package cn.lopr.im.android.mvp.presenter;

import android.text.TextUtils;

import cn.lopr.im.android.bean.CallbackResult;
import cn.lopr.im.android.bean.User;
import cn.lopr.im.android.common.OnCallBackListener;
import cn.lopr.im.android.common.app.Activity;
import cn.lopr.im.android.mvp.contact.IRegisterContact;
import cn.lopr.im.android.mvp.model.RegisterModel;
import cn.lopr.im.android.utils.UiUtils;

/**
 * @author shuangyang7
 * @date 2020/12/19 11:51
 */
public class RegisterPresenterImpl implements IRegisterContact.IRegisterPresenter {

    /**
     * 视图接口对象
     */
    private final IRegisterContact.IRegisterView registerView;
    private final IRegisterContact.IRegisterModel registerModel;
    private final Activity activity;

    /**
     * 在构造方法中实例化model对象
     */
    public RegisterPresenterImpl(Activity activity, IRegisterContact.IRegisterView registerView) {
        this.registerModel = new RegisterModel();
        this.registerView = registerView;
        this.activity = activity;
    }

    @Override
    public void getCode() {
        if (check()) {
            return;
        }

        if (!UiUtils.isNetworkAvailable()) {
            registerView.showNetworkError();
            return;
        }

        registerView.showLoading(this.activity);

        registerModel.getCode(registerView.getPhone(), new OnCallBackListener<CallbackResult<User>, String>() {
            @Override
            public void success(CallbackResult<User> result) {
                registerView.disLoading();
                registerView.regFailure("获取验证码成功！");
            }

            @Override
            public void failure(String errMsg) {
                registerView.disLoading();
                registerView.regFailure(errMsg);
            }
        });
    }

    @Override
    public void reg() {

        if (check()) {
            return;
        }

        if (!UiUtils.isNetworkAvailable()) {
            registerView.showNetworkError();
            return;
        }

        registerView.showLoading(this.activity);

        registerModel.reg(registerView.getPhone(), registerView.getPassword(), registerView.getName(),
                new OnCallBackListener<CallbackResult<User>, String>() {
                    @Override
                    public void success(CallbackResult<User> result) {
                        registerView.disLoading();
                        registerView.regSuccess(result.getData());
                    }

                    @Override
                    public void failure(String errMsg) {
                        registerView.disLoading();
                        registerView.regFailure(errMsg);
                    }
                });
    }

    public boolean check() {
        if (TextUtils.isEmpty(registerView.getName())) {
            registerView.regFailure("昵称不能为空");
            return true;
        }
        if (TextUtils.isEmpty(registerView.getPhone())) {
            registerView.regFailure("手机号不能为空");
            return true;
        }
        if (TextUtils.isEmpty(registerView.getPassword())) {
            registerView.regFailure("密码不能为空");
            return true;
        }
        if (registerView.getName().length() >= 15) {
            registerView.regFailure("名字太长，请输入15位以内的字符");
            return true;
        }

        return false;
    }

}
